import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutterlib/common/constants/constants.dart';
import 'package:flutterlib/common/net/dao_result.dart';
import 'package:flutterlib/common/net/retrofit/activity_api.dart';
import 'package:flutterlib/common/style/quhui_style.dart';
import 'package:flutterlib/common/utils/utils.dart';
import 'package:flutterlib/model/activity/activity_page_query.dart';
import 'package:flutterlib/pages/base/base_sliver_list.dart';
import 'package:flutterlib/widgets/back_button.dart';
import 'package:flutterlib/widgets/bussiness/activity/activity_list_item_new.dart';
import 'package:flutterlib/widgets/pull/nested/simple_pull_load_manger.dart';
import 'package:flutterlib/widgets/pull/nested/sliver_header_delegate.dart';
import 'package:flutterlib/widgets/quhui_select_item_widget.dart';

class Page extends BaseSliverList {
  Page({Key key}) : super(key: key);

  @override
  ActivityDeployListPageState createState() => ActivityDeployListPageState();
}

///页面 KeepAlive ，同时支持 动画Ticker
class ActivityDeployListPageState extends BaseSliverListState<Page> {
  SimplePullLoadManger manger;
  final Map<int, dynamic> choiceSelectMap = new Map();
  final SettingsStore store =
      Provider.of<SettingsStore>(currentContext, listen: false);

  ///滑动监听
  final ScrollController scrollController = new ScrollController();

  getDataLogic(int page) async {
    int isOnline = choiceSelectMap[0] != null ? (choiceSelectMap[0] - 1) : null;
    int isCompany =
        (choiceSelectMap[2] != null && choiceSelectMap[2] == 2) ? 1 : null;
    var activitys = await ActivityApi.builder().pageByNewQuery(
      ActivityPageQuery(
        interestId: 0,
        type: 1,
        userId: currentUserId,
        isOnline: isOnline,
        pageNum: page,
        isCompany: isCompany,
        orderField: getOrderFiledByChoice(choiceSelectMap[3]),
      ),
    );
    return new DataResult(activitys.list, true);
  }

  Widget renderItem(index, activity) {
    Poi point;
    if (store.myPosition != null) {
      point = Poi(
          longitude: store.myPosition.longitude.toString(),
          latitude: store.myPosition.latitude.toString());
    }
    return ActivityListItemNew(activity, myLocation: point, onPressed: () {
      NavigatorUtils.gotoActivityDetail(context, activity.id);
    });
  }

  @override
  List<Widget> renderHeader(BuildContext context, bool innerBoxIsScrolled) {
    return <Widget>[
      ///动态放大缩小的tab控件
      SliverPersistentHeader(
        pinned: true,

        /// SliverPersistentHeaderDelegate 的实现
        delegate: SliverHeaderDelegate(
            maxHeight: 60,
            minHeight: 60,
            changeSize: true,
            snapConfig: FloatingHeaderSnapConfiguration(
              vsync: this,
              curve: Curves.bounceInOut,
              duration: const Duration(milliseconds: 10),
            ),
            builder: (BuildContext context, double shrinkOffset,
                bool overlapsContent) {
              ///根据数值计算偏差
              var radius = Radius.circular(
                  0); //Radius.circular(4 - shrinkOffset / 60 * 4);
              Widget sizeBox = SizedBox.expand(
                child: Padding(
                  padding: EdgeInsets.only(bottom: 10, left: 0, right: 0),
                  child: new QuhuiSelectItemWidget(
                    (tabIndex, choiceIndex) {
                      ///切换时先滑动
                      choiceSelectMap[tabIndex] = choiceIndex;
                      manger.refresh();
//                            showRefreshLoading();
                    },
                    margin: EdgeInsets.zero,
                  ),
                ),
              );

              return sizeBox;
            }),
      ),
    ];
  }

  @override
  renderActions() {
    return <Widget>[];
  }

  @override
  Widget renderLeading() {
    return MyBackButton(
      color: Colors.black,
    );
  }

  @override
  Widget renderTitle() {
    return Text("招募活动", style: QuhuiConstant.normalTextBold);
  }

  getOrderFiledByChoice(choiceSelectMap) {
    if (choiceSelectMap == null || choiceSelectMap == 0 || choiceSelectMap == 1)
      return null;
    if (choiceSelectMap == 2) return 'distance';
  }

  @override
  bool enablePullDown() {
    return true;
  }

  @override
  bool enablePullUp() {
    return true;
  }
}
